<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>礼包详情</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all" />
</head>
<body>
		<form class="layui-form" action="">
		<table class="layui-table">
			<colgroup>
				<col width="150">
				<col width="200">
				<col>
			</colgroup>
			<thead>
				<tr>
					<th>客户姓名</th>
					<th>手机号码</th>
					<th>选择保单</th>
					<th>预返金额(元)</th>
				</tr>
			</thead>
			<tbody>
				<tr>

					<td id="memberName" data-id=""></td>
					<td id="memberPhone"></td>
					<td id="policyId"></td>
					<td id="returnMoney"></td>
				</tr>
			</tbody>
		</table>
		
		<table class="layui-table">
			<colgroup>
				<col width="150">
				<col width="200">
				<col>
			</colgroup>
			<thead>
				<tr>
					<th>产品类型</th>
					<th>产品名称</th>
					<th>产品单价(元)</th>
					<th>产品描述</th>
					<th>发送数量</th>
					<th>使用期限(年)</th>
				</tr>
			</thead>
			<tbody id="sendList">
				<tr id="emptyRow">
					<td colspan="7" align="center">列表空空的,什么也没有</td>
				</tr>
			</tbody>
		</table>

		<div class="layui-form-item" id="welfareList"></div>
	</form>
	
	<script type="text/javascript" src="/plugins/layui/layui.js"></script>
		<script type="text/javascript">
layui.config({
    base: '/js/admin/'
}).extend({
    ajaxExtention: 'ajaxExtention',//加载自定义扩展，每个业务js都需要加载这个ajax扩展
    $tool: 'tool'
}).use(['form', 'layer', 'tree', 'jquery', 'ajaxExtention', '$tool'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery,
        $tool = layui.$tool;

    var queryArgs = $tool.getQueryParam();//获取查询参数
    var id = queryArgs['id'];
      initWelfareInfo();
	  initGiftPackageInfo();
	  
    	//计算当前 表里元素的总价格
    	
    	function calculate(){
    		var total=0;
    		 if($("#sendList:has(tr)").length==0){
    			 return 0;
    		 }else{
    			 $("#sendList .valid_tr").each(function(){
    				var price= $(this).find(".c_price").html();
    				var count= $(this).find(".c_count").html();
    				total+=price*count;
    			 });
    		 }
    		 return total;
    	}
    
    function initWelfareInfo() {
    	var url="/welfares/getByRegion";
        $.ajax({
            url:url,
            method:"GET",
            success:function (data) {
                var welfares = data.data;
                if(welfares!=null && welfares.length>0){
                	 var html='<label class="layui-form-label">地市福利</label>'+
                     '<div class="layui-input-block">';
                	 for(var i=0;i<welfares.length;i++){	
                		 html += '<input type="checkbox" data-id="'+welfares[i].id+'" title="'+welfares[i].welfareName+'" readonly>';
                   		}  
                	 html +='</div>';
                	 $("#welfareList").html(html);
                	 
                }else{
                	 $("#welfareList").html('');
                }
                form.render();//重新绘制表单，让修改生效 
            }
        });
    }
    
    
     function initGiftPackageInfo() {
        var queryArgs = $tool.getQueryParam();//获取查询参数
        var id = queryArgs['id'];
        var url = '/giftPackages/getById';
        var req = {id:id};
        $.ajax({
            url:url,
            method:"POST",
            data:req,
         //   dataType:'json',
            success:function (data) {
                var member = data.member;
                var policy=data.policy;
                var productDtos=data.productDtos;
                var ownerWelfares=data.ownerWelfares;
               $("#memberName").html(member.name);
              // $("#memberName").attr("data-id",datas.memberId);
               $("#memberPhone").html(member.phone);
               if(policy!=null){
            	   $("#policyId").html(policy.code);
            	   $("#returnMoney").html(policy.returnMoney);
               }else{
            	   $("#policyId").html('无');	  
               }
               
             	 var html='';
             	 if(productDtos!=null && productDtos.length>0){
             		for(var i=0;i<productDtos.length;i++){	
             			 html+='<tr class="valid_tr">'+
               	      '<td>'+productDtos[i].categoryName+'</td>'+
               	      '<td>'+productDtos[i].name+'</td>'+
               	      '<td class="c_price">'+productDtos[i].price+'</td>'+
               	      '<td>'+productDtos[i].description+'</td>'+
               	      '<td class="c_count">'+productDtos[i].count+'</td>'+
               	      '<td >'+productDtos[i].usePeriod+'</td>'+
               	      ' </tr>';	
             		}
             		 if($("#emptyRow").length>0){
           	    	  $("#emptyRow").remove();
           	      	}
             		if($("#totalRow").length==0){
              		  $("#sendList").append('<tr id="totalRow"><td colspan="7" align="center" id="totalPrice"></td></tr>')
              	  	}
             		
             		 $("#sendList tr:last").before(html);
             		$("#totalPrice").html("合计:"+calculate()+"元");
             	 }
             	 
             	 //选中地市福利
             	 for(var i=0;i<ownerWelfares.length;i++){	
             	$('#welfareList input[data-id='+ownerWelfares[i].welfareId+']').attr('checked', true);
             	
             	
             	$('#welfareList input[data-id='+ownerWelfares[i].welfareId+']').attr('link-id', ownerWelfares[i].linkId);
             		// $('#welfareList input["data-id='+ownerWelfares[i]+'"]').attr('checked', true);
             	 }
             	 
            	     
                form.render();//重新绘制表单，让修改生效 
            }
        });
       
    }
});

</script>
	
</body>
</html>